package demo.practice.tags.binary_search;

public class P704_2 {

    public int search(int[] nums, int target) {
        if (nums == null)
            return -1;
        int min = 0;
        int max = nums.length - 1;

        while (max>=min){
            int middle= min+(max-min)/2;
            if(nums[middle]==target)
                return middle;
            if(nums[middle]>target)//在middle前面，那middle=1则是下一次的最大值
                max=middle-1;
            else
                min=middle+1;   //nums[middle]<target，那么target在middle前面，middle+1 变成下一次的最小值
        }
        return -1;
    }
}
